package com.lancoo.znbkxx.teacher.ui.activity

import android.webkit.*
import com.lancoo.cpbase.authentication.base.CurrentUser
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.Ext.Companion.COURSE_CLASSID
import com.lancoo.znbkxx.teacher.Ext.Companion.GLOBAL_GRADE
import com.lancoo.znbkxx.teacher.Ext.Companion.PREDICTION_URL
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.base.BaseVMActivity
import com.lancoo.znbkxx.teacher.base.BaseViewModel
import com.lancoo.znbkxx.teacher.databinding.ActivityAnalysisBinding

/**
 * @author : wealive
 * @date   : 2020/4/1 15:48
 * @desc   :
 */
class PredictionActivity : BaseVMActivity<BaseViewModel, ActivityAnalysisBinding>() {
    override fun setLayoutID(): Int {
        return R.layout.activity_analysis
    }

    override fun providerVMClass(): Class<BaseViewModel>? {
        return BaseViewModel::class.java
    }

    override fun initView() {
        binding.tvTitle.text = getString(R.string.performance_prediction)
        binding.sbtnBack.setOnClickListener {
            if (binding.wv.canGoBack()) {
                binding.wv.settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
                binding.wv.goBack()
                binding.tvTitle.text = getString(R.string.performance_prediction)
            } else {
                finish()
            }
        }
        binding.wv.apply {
            settings.javaScriptEnabled = true
            settings.useWideViewPort = true
            settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
            settings.loadWithOverviewMode = true
            addJavascriptInterface(JS(), "Android")
            webChromeClient = WebChromeClient()
            webViewClient = WebViewClient()
            var subjectID = ""
            val juniorList = arrayOf("K6","K7", "K8", "K9")
            val seniorList = arrayOf("K10", "K11", "K12")
            if (juniorList.contains(GLOBAL_GRADE)) {
                subjectID = "B"
            }
            if (seniorList.contains(GLOBAL_GRADE)) {
                subjectID = "C"
            }
            loadUrl("${PREDICTION_URL}build/index.html#/OutPerdictFrame/?SchoolID=${CurrentUser.SchoolID}&SubjectID=S2-English&HTestID=$subjectID&CourseClassID=$COURSE_CLASSID&SystemType=2&TeacherID=${CurrentUser.UserID}")
        }
    }

    internal inner class JS {
        @JavascriptInterface
        fun getStuScoreDetail(url: String) {
            runOnUiThread {
                when (url) {
                    "1" -> {
                        binding.wv.settings.cacheMode = WebSettings.LOAD_DEFAULT
                        binding.wv.goBack()
                        binding.tvTitle.text = getString(R.string.performance_prediction)
                    }
                    "0" -> {
                        binding.wv.settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
                        binding.wv.goBack()
                        binding.tvTitle.text = getString(R.string.performance_prediction)
                    }
                    else -> {
                        binding.wv.settings.cacheMode = WebSettings.LOAD_DEFAULT
                        binding.wv.loadUrl(url)
                        binding.tvTitle.text = "学生作答详情"
                    }
                }
            }
        }
    }
}


